<?php
global $_GPC, $_W;
load()->func('tpl');
$GLOBALS['frames'] = $this->getMainMenu($storeid,$action);
//var_dump($GLOBALS['frames']);die;
$type = $_GPC['type']?$_GPC['type']:0;
$where=" where a.uniacid=:uniacid and a.del = 0";
$data[':uniacid']=$_W['uniacid'];
if(!empty($type)){
    $where .= " and a.status = :status";
    $data[':status']=$type;
}
if(!empty($_GPC['keywords'])){
  $where.=" and (a.yd_name LIKE  concat('%', :name,'%') || a.order_sn LIKE  concat('%', :name,'%') || b.name LIKE  concat('%', :name,'%'))";
  $data[':name']=$_GPC['keywords'];
}
if($_GPC['time']){
  $start=strtotime($_GPC['time']['start']);
  $end=strtotime($_GPC['time']['end']);
  $where.=" and UNIX_TIMESTAMP(a.yd_time) >={$start} and UNIX_TIMESTAMP(a.yd_time)<={$end}";
}
$pageindex = max(1, intval($_GPC['page']));
$pagesize=10;
$sql="SELECT a.*,b.name as md_name FROM ".tablename('cjdc_ydorder'). " a"  . " left join " . tablename("cjdc_table_type") . " b on a.tabletype_id=b.id ".$where." ORDER BY a.yd_time DESC";
$total=pdo_fetchcolumn("SELECT count(*) FROM ".tablename('cjdc_ydorder'). " a"  . " left join " . tablename("cjdc_store") . " b on a.store_id=b.id  ".$where." ORDER BY a.yd_time DESC",$data);
$select_sql =$sql."  LIMIT " .($pageindex - 1) * $pagesize.",".$pagesize;
$list=pdo_fetchall($select_sql,$data);
if(!empty($list)){
    foreach ($list as $key=>$value){
        $list[$key]['create_time'] = date('Y-m-d H:i',$value['create_time']);
        $list[$key]['yd_time'] = date('Y-m-d H:i',$value['yd_time']);
        if(!empty($value['com_time'])){
            $list[$key]['com_time'] = date('Y-m-d H:i',$value['com_time']);
        }

    }
}
$pager = pagination($total, $pageindex, $pagesize);
if($_GPC['op']=='ok'){
  pdo_delete('cjdc_formid',array('time <='=>time()-60*60*24*7));
  $data2['yy_state']=3;
  // $data2['completion_time']=time();
  $rst=pdo_update('cjdc_order',$data2,array('id'=>$_GPC['id']));
  if($rst){
    file_get_contents("".$_W['siteroot']."app/index.php?i=".$_W['uniacid']."&c=entry&a=wxapp&do=addintegral&m=zh_cjdianc&type=4&order_id=".$_GPC['id']);
    //有效分销佣金
    $this->updcommission($_GPC['id']);
    ///////////////模板消息通过///////////////////
    function getaccess_token($_W){
      $res=pdo_get('cjdc_system',array('uniacid'=>$_W['uniacid']));
      $appid=$res['appid'];
      $secret=$res['appsecret'];
      $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret."";
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL,$url);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
      $data = curl_exec($ch);
      curl_close($ch);
      $data = json_decode($data,true);
      return $data['access_token'];
    }
    //设置与发送模板信息
    function set_msg($_W){
      $access_token = getaccess_token($_W);
      $res=pdo_get('cjdc_message',array('uniacid'=>$_W['uniacid']));
      $res2=pdo_get('cjdc_order',array('id'=>$_GET['id']));
      $user=pdo_get('cjdc_user',array('id'=>$res2['user_id']));
      $store=pdo_get('cjdc_store',array('id'=>$res2['store_id']));
      $table=pdo_get('cjdc_table_type',array('id'=>$res2['table_id']));
      $form=pdo_get('cjdc_formid',array('user_id'=>$res2['user_id'],'time >='=>time()-60*60*24*7));
      $formwork ='{
           "touser": "'.$user["openid"].'",
           "template_id": "'.$res["yy_tid"].'",
           "page": "zh_cjdianc/pages/Liar/loginindex",
           "form_id":"'.$form['form_id'].'",
           "data": {
             "keyword1": {
               "value": "'.$store['name'].'",
               "color": "#173177"
             },
             "keyword2": {
               "value":"'.$res2['order_num'].'",
               "color": "#173177"
             },
             "keyword3": {
               "value": "'.$res2['tel'].'",
               "color": "#173177"
             },
             "keyword4": {
               "value":  "'.$res2['tableware'].'",
               "color": "#173177"
             },
             "keyword5": {
               "value": "'.$table['name'].'",
               "color": "#173177"
             },
             "keyword6": {
               "value": "'.$res2['time'].'",
               "color": "#173177"
             },
             "keyword7": {
               "value": "'.$res2['money'].'",
               "color": "#173177"
             },
              "keyword8": {
               "value": "'.$res2['delivery_time'].'",
               "color": "#173177"
             },
              "keyword9": {
               "value": "预约通过,请在规定时间前往就餐~",
               "color": "#173177"
             }
           }
         }';
      // $formwork=$data;
      $url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=".$access_token."";
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL,$url);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
      curl_setopt($ch, CURLOPT_POST,1);
      curl_setopt($ch, CURLOPT_POSTFIELDS,$formwork);
      $data = curl_exec($ch);
      curl_close($ch);
      // return $data;
      pdo_delete('cjdc_formid',array('id'=>$form['id']));
    }
    echo set_msg($_W);
    ///////////////模板消息///////////////////
    message('确认成功',$this->createWebUrl('ydorder',array()),'success');
  } else{
    message('确认失败','','error');
  }
}
if($_GPC['op']=='delete'){
  $res=pdo_delete('cjdc_ydorder',array('id'=>$_GPC['id']));
  if($res){
      add_log($_W['username'],'删除订单');
    message('删除成功！', $this->createWebUrl('newydorder'), 'success');
  } else{
    message('删除失败！','','error');
  }
}

include $this->template('web/newydorder');